#include "DynamicLibrary.h"


CDynamicLibrary::CDynamicLibrary() :m_hModule(NULL)
{
}


CDynamicLibrary::~CDynamicLibrary()
{
	Close();
}

bool CDynamicLibrary::Open(const char *lpname)
{
#ifdef WIN32
	m_hModule = LoadLibraryA(lpname);
#else
	m_hModule = dlopen(lpname, RTLD_NOW | RTLD_GLOBAL);
#endif 
	return m_hModule != NULL;
}

void CDynamicLibrary::Close()
{
#ifdef WIN32
	FreeLibrary(m_hModule);
#else
	dlclose(m_hModule);
#endif 
	m_hModule = NULL;
}

void * CDynamicLibrary::GetProc(const char *pfname)
{
	if (m_hModule)
	{
#ifdef WIN32
		return (void *)GetProcAddress(m_hModule, pfname);
#else
		return dlsym(m_hModule, pfname);
#endif 
	}

	return  NULL;
}

const char * CDynamicLibrary::GetLastErrorMsg()
{
	char szMsg[128];
#ifdef WIN32
	sprintf(szMsg, "dll error(%u)", ::GetLastError());
#else
	sprintf(szMsg, "%s", dlerror());
#endif 
	strErrMsg = szMsg;
	return strErrMsg.c_str();
}
